import Vue from 'vue'
import VueRouter from 'vue-router'
import recommend from '../views/recommend.vue'
Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/recommend'
  },
  {
    path: '/recommend',
    name: 'recommend',
    component: recommend,
  }, {
    path: '/recommend/detail/:id?',
    name: 'detail',
    component: () => import("@/components/recommend/detail.vue"),
  }, {
    path: "/recommend/video/:mvid",
    name: 'video',
    component: () => import('@/components/recommend/video.vue')
  }, {
    path: '/rank',
    name: 'rank',
    component: () => import("@/views/rank.vue")
  }, {
    path: '/singer',
    name: 'singer',
    component: () => import("@/views/singer.vue"),
  }, {
    path: '/singer/songs/:id?',
    name: 'songs',
    component: () => import("@/components/singer/songs.vue"),
  }, {
    path: '/login',
    name: 'login',
    component: () => import("@/views/login.vue"),
  }, {
    path: '/login/phone:type?',
    name: 'phone',
    component: () => import("@/components/login/phone.vue"),
  }, {
    path: '/search',
    name: 'search',
    component: () => import("@/views/search.vue"),
  },
  {
    path: "/userInfo",
    name: "userInfo",
    component: () => import("@/views/userinfo.vue"),
  }, {
    path: "/login/register",
    name: 'register',
    component: () => import('@/components/login/register.vue')
  }, {
    path: "/userInfo/follows",
    name: 'follows',
    component: () => import('@/components/userInfo/follows.vue')
  }, {
    path: "/userInfo/cloudDisk",
    name: 'cloudDisk',
    component: () => import('@/components/userInfo/cloudDisk.vue')
  },
  {
    path: "/userInfo/HistoryPlay",
    name: 'HistoryPlay',
    component: () => import('@/components/userInfo/HistoryPlay.vue')
  }, {
    path: "/userInfo/purchased",
    name: 'purchased',
    component: () => import('@/components/userInfo/purchased.vue')
  }, {
    path: "/userInfo/collection/:id?",
    name: 'collection',
    component: () => import('@/components/userInfo/collection.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
const whiteList = ['/login', '/login/phone', '/login/register'] // 定义白名单
router.beforeEach((to, from, next) => {
  let login = window.localStorage.getItem("token");
  // 登陆界面
  if (whiteList.indexOf(to.path) > -1) {
    next();
    return;
  } else {
    if (!login) {
      next('/login');
      return;
    } else {
      next();
    }
  } // 判断是否登陆
})
export default router